package com.aps.common.utils.converter;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.aps.common.model.genetic.Order;
import com.aps.common.model.genetic.OrderProcess;
import com.aps.common.model.standardAps.StandardApsOrder;
import com.aps.common.model.standardAps.StandardApsOrderProcess;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author jsy
 * @description: 订单转换器
 * @date 2025/9/22 14:19
 */
public class OrderConverter {
    /**
     * @description: 将List<ApsOrder>转换为List<Order>
     * @author: jsy
     * @time: 2025/9/22 14:11
     */
    public static List<Order> convert(List<StandardApsOrder> apsOrders) {
        // 处理null情况，返回空列表而不是null更安全
        if (CollUtil.isEmpty(apsOrders)) {
            return Collections.emptyList();
        }

        // 使用Stream进行转换
        return apsOrders.stream()
                .filter(ObjectUtil::isNotEmpty)  // 过滤null元素
                .map(OrderConverter::convertSingle)  // 转换单个对象
                .collect(Collectors.toList());
    }

    /**
     * @description: 将单个ApsOrder转换为Order
     * @author: jsy
     * @time: 2025/9/22 14:17
     */
    private static Order convertSingle(StandardApsOrder apsOrder) {
        Order order = new Order();

        // 手动映射所有字段，这里假设了一些常见字段，实际应根据真实类结构修改
        order.setId(apsOrder.getId());
        order.setQuantity(apsOrder.getQuantity());
        order.setDueDate(apsOrder.getDueDate());
        order.setStartDate(apsOrder.getStartDate());
        // 处理嵌套列表
        order.setProcessList(convertProcesses(apsOrder.getOrderProcessList()));

        return order;
    }

    /**
     * @description: 转换工序列表
     * @author: jsy
     * @time: 2025/9/22 14:22
     */
    private static List<OrderProcess> convertProcesses(List<StandardApsOrderProcess> apsProcesses) {
        if (Objects.isNull(apsProcesses)) {
            return List.of();
        }

        return apsProcesses.stream()
                .filter(Objects::nonNull)
                .map(process -> {
                    OrderProcess orderProcess = new OrderProcess();
                    // 手动映射工序字段
                    orderProcess.setId(process.getId());
                    orderProcess.setOrderId(process.getOrderId());
                    orderProcess.setEquipmentType(process.getEquipmentType());
                    orderProcess.setProcessName(process.getProcessName());
                    orderProcess.setSequence(process.getSequence());
                    orderProcess.setUnitProcessingTime(process.getUnitProcessingTime());
                    orderProcess.setOperationPreparedTime(process.getOperationPreparedTime());
                    return orderProcess;
                })
                .collect(Collectors.toList());
    }

    /**
     * @description: 将订单列表转换为以订单ID为key、订单对象为value的Map，若存在相同ID的订单，默认保留最后出现的订单
     * @author: jsy
     * @time: 2025/9/23 15:38
     */
    public static Map<String, Order> convertToMap(List<Order> orders) {
        // 处理空列表，避免空指针异常
        if (orders == null) {
            return Collections.emptyMap();
        }

        // 使用toMap转换：key为Order的id，value为Order本身；重复key时保留后者
        return orders.stream()
                .collect(Collectors.toMap(
                        Order::getId,  // key生成器：取订单ID
                        Function.identity(),  // value生成器：直接使用订单对象
                        (existing, replacement) -> replacement  // 重复key时的合并策略：保留新值
                ));
    }
}
